package LUDOSimulator;

/**
 * Made by - 
 * ISHAN GANESHAN,Roberto Edmundo Ponce Reyes ,Cristian Florian Mitu 
 * 
 * 
**/

public class IshanLUDOPlayer implements LUDOPlayer {
	
	double TwH1_I1=-0.06260063588460131,TwH1_I2=-4.652088218934489,TwH1_I3=0.32824642122850806,TwH1_I4=-0.7642359947865884,
	TwH1_I5=0.20214831460524382,TwH1_I6=-1.5320448865933174,TwH1_I7=-2.7034329783276263,TwH1_B=0.1875789415193735,
	TwH2_I1=1.113429958103957,TwH2_I2=-11.995118511853923,TwH2_I3=-1.7100271377342569,TwH2_I4=2.9393702524429752,
	TwH2_I5=-1.3263877151974104,TwH2_I6=2.8090086616064363,	TwH2_I7=2.3303515823241554,TwH2_B=1.6637174822289296,
	TwH3_I1=-0.9203769330135703,TwH3_I2=7.891342329429428,TwH3_I3=1.3848227212615065,TwH3_I4=-2.1770715236199045,
	TwH3_I5=1.1118261818201844,TwH3_I6=-2.3096987621263647,TwH3_I7=-2.55322035857009,TwH3_B=0.5846469209429068,
	TwH4_I1=-2.2860410833205527,TwH4_I2=3.2526517315422296,TwH4_I3=0.15333225714117926,TwH4_I4=1.994808865251952,
	TwH4_I5=0.606510059793712,TwH4_I6=1.9651634391014345,TwH4_I7=2.3755917278334406,TwH4_B=2.1142224895674673,
	TwO1_H1=-5.542183597013615,TwO1_H2=4.5637798287828115,TwO1_H3=2.7437139283742504,TwO1_H4=-2.401653795849109,
	TwO1_B=-0.721121752037197,IH1,IH2,IH3,IH4,IH5,IH6,OH1,OH2,OH3,OH4,OH5,OH6,IO1,IO2,IO3,IO4,IO5,actualO1,actualO2,actualO3,Error1,Error2,
     actualO4,actualO5,Error;
	int bias=1;
	int I1,I2,I3,I4,I5,I6,I7;

	int brickGlobe, brickStrikeOpponent,brickStar,brickAtStart,brickAtNormalpos,brickAlmosthomepos,brickAthomepos;
	
	LUDOBoard board;
	
	private boolean yinyang;
	public IshanLUDOPlayer(LUDOBoard board){
		this.board = board;
		
	}
	
	public void play(){
		
	
		
		board.print("Ishan Ganeshan playing");
		board.rollDice();
		
		
		I1=0;
		I2=0;
		I3=0;
		I4=0;
		I5=0;
		I6=0;
		I7=0;
		for(int i=0;i<4;i++)
		{
			analyze(i); 
			
		}
		
		
		finalcalc();
		
		makeMove();
		
	
		
	}
	
	public void analyze(int i){
		int diceValue = board.getDice();
		if(board.moveable(i)) {
			int[][] currentState = board.getBoardState();
			int[][] newState = board.getNewBoardState(i, board.getMyColor(), diceValue);
							
			if(board.isGlobe(newState[board.getMyColor()][i])){
							
					yinyang = blankGlobe(currentState,newState);
					
				if(yinyang) {
					I1 = 1;
					
				brickGlobe = i;}
				
					
			}
			
			else if((strikeOpponent(currentState,newState))){
				I2 = 1;
				brickStrikeOpponent = i;
				}
			else if(board.isStar(newState[board.getMyColor()][i])){
				I3 = 1;
				
				brickStar = i;
				
			}
				
			
			
			 if(moveOut(currentState,newState)){
				
				I4 = 1;
				
				brickAtStart = i;
			}
	
			
			 else if((!board.almostHome(currentState[board.getMyColor()][i],board.getMyColor()) && board.almostHome(newState[board.getMyColor()][i],board.getMyColor()))){
				 
				I6=1;
				
				brickAlmosthomepos = i;
			}
			
			 else if(board.atHome((newState[board.getMyColor()][i]),board.getMyColor())){
				
				I7=1;
				
				brickAthomepos = i;
			}
			else if(board.atField(newState[board.getMyColor()][i])){
				I5=1;
				
				brickAtNormalpos = i;
			}

		}
	}// end of analyze
		
			
			
	private boolean moveOut(int[][] currentState, int[][] newState) {
		for(int j=0;j<4;j++) {
			if(board.inStartArea(currentState[board.getMyColor()][j],board.getMyColor())&&!board.inStartArea(newState[board.getMyColor()][j],board.getMyColor()) && board.getDice()==6) {
				
				
			return true;
			}
		}
		return false;
		
	}
	
		
		
		private boolean blankGlobe(int[][] currentState,int[][]newState){
			
			for(int x=0;x<4;x++){
				for(int y=0;y<4;y++){
					if(board.getMyColor()==x){
											if(board.isGlobe(currentState[x][y]) && board.isGlobe(newState[x][y])){// && (newState[board.getMyColor()][y] != 0 && newState[board.getMyColor()][y] != 13 && newState[board.getMyColor()][y] != 26 && newState[board.getMyColor()][y] != 39)){
						
							return true;
						}
						
				}
				}
			}
		return false;			
		}
	
		
		private boolean strikeOpponent(int[][] currentState,int[][] newState){
			
			for(int x=0;x<4;x++){
				for(int y=0;y<4;y++){
					if(board.getMyColor()!=x){
						if(board.atField(currentState[x][y]) && !board.atField(newState[x][y])){
						return true; // opponent strike out possible
						}
						
						}
				}
			}
			return false;		
		}
		
	
	public void finalcalc(){
		
		IH1 = I1 * TwH1_I1 + I2 * TwH1_I2 + I3 * TwH1_I3 + I4 * TwH1_I4 + I5 * TwH1_I5 + I6 * TwH1_I6 + I7 * TwH1_I7 + bias * TwH1_B; 
		IH2 = I1 * TwH2_I1 + I2 * TwH2_I2 + I3 * TwH2_I3 + I4 * TwH2_I4 + I5 * TwH2_I5 + I6 * TwH2_I6 + I7 * TwH2_I7 + bias * TwH2_B;
		IH3 = I1 * TwH3_I1 + I2 * TwH3_I2 + I3 * TwH3_I3 + I4 * TwH3_I4 + I5 * TwH3_I5 + I6 * TwH3_I6 + I7 * TwH3_I7 + bias * TwH3_B; 
		IH4 = I1 * TwH4_I1 + I2 * TwH4_I2 + I3 * TwH4_I3 + I4 * TwH4_I4 + I5 * TwH4_I5 + I6 * TwH4_I6 + I7 * TwH4_I7 + bias * TwH4_B; 
		
		
		OH1 = 1/(1+ Math.exp(-IH1));
		OH2 = 1/(1+ Math.exp(-IH2));
		OH3 = 1/(1+ Math.exp(-IH3));
		OH4 = 1/(1+ Math.exp(-IH4));

		IO1 = OH1 * TwO1_H1 + OH2 * TwO1_H2 + OH3 * TwO1_H3 + OH4 * TwO1_H4 +bias * TwO1_B;
		actualO1 = 1/(1+Math.exp(-IO1));
		
		
		actualO1 = 1000*actualO1;
		
	}
		

	public void makeMove(){
		
		if(actualO1>530 && actualO1<664){
				board.moveBrick(brickGlobe);
				
				
			}
		
		else if(actualO1>665 && actualO1<730){
			board.moveBrick(brickAlmosthomepos);
			
			
		}


		else if(actualO1>750){
			board.moveBrick(brickAthomepos);
			
		}
		
		
		else if(actualO1<250 && actualO1>201){
			board.moveBrick(brickAtNormalpos);
			
		
		}
		
				
		else if(actualO1>470 && actualO1<524){
				board.moveBrick(brickAtStart);
							}
			
		else if(actualO1<200){
				board.moveBrick(brickStar);
				
			
			}
		
			
		else if(actualO1>350 && actualO1<435){
				board.moveBrick(brickStrikeOpponent);
				
			}
			
			}
			
		
	
	
}
























		